package jp.business.utils.logic;

import jp.business.utils.logic.connector.BranchHandle;
import jp.business.utils.logic.connector.DefaultExceptionFunction;
import jp.business.utils.logic.connector.TrueHandle;
import jp.business.utils.logic.exception.ServerException;

/**
 * @className: LogicTools
 * @description: 可以用于异常处理的逻辑工具，适用于if逻辑
 * @author: 云野
 * @date: 2021/12/2
 */
public class LogicTools {

  /**
   * @param: b
   * @description: 如果参数为true抛出异常
   * @return: jp.function.utils.function.myInterface.function.DefaultExceptionFunction
   * @author: Jp
   * @date: 2021/12/2
   */
  public static DefaultExceptionFunction isTureForDefaultException(boolean b) {

    return (errorMessage) -> {
      if (b) {
        throw new ServerException(errorMessage);
      }
    };
  }
  /**
   * @param: b
   * @description: 参数为true，进行操作
   * @return: jp.function.utils.function.myInterface.function.TrueHandle
   * @author: Jp
   * @date: 2021/12/2
   */
  public static TrueHandle isTure(boolean b) {

    return (trueHandle) -> {
      if (b) {
        trueHandle.run();
      }
    };
  }

  /**
   * @param: b
   * @description: 参数为true或false时，分别进行不同的操作
   * @return: jp.function.utils.function.myInterface.function.BranchHandle
   * @author: Jp
   * @date: 2021/12/2
   */
  public static BranchHandle isTureOrFalse(boolean b) {

    return (trueHandle, falseHandle) -> {
      if (b) {
        trueHandle.run();
      } else {
        falseHandle.run();
      }
    };
  }
}
